package com.linkhub.oms.admin.modules.satoken.session;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.linkhub.oms.admin.modules.satoken.domain.SaUserInfoDTO;
import com.linkhub.oms.admin.modules.user.domain.dto.UserDTO;
import com.linkhub.oms.admin.modules.user.service.UserBizService;
import com.linkhub.oms.admin.modules.user.service.UserRoleBizService;
import com.linkhub.oms.admin.modules.whitelist.service.WhiteListBizService;
import com.linkhub.oms.common.constant.RoleConstant;
import com.linkhub.oms.common.constant.SaTokenConstant;
import com.linkhub.oms.common.spring.QtApplicationContextHelper;
import com.linkhub.oms.dao.mpservice.IShopService;

import java.util.List;

/**
 * @Description: LhSessionContext，只允许在web环境中调用
 * @Author: echo
 * @Date: 2024/3/8 23:13
 * @Version: 1.0
 */
public class LhSessionContext {

    /**
     * 获取当前登录用户的 userId
     *
     * @return userId
     */
    public static String getUserId() {
        return String.valueOf(StpUtil.getLoginId());
    }

    /**
     * 获取当前登录用户的 email
     *
     * @return email
     */
    public static String getUserEmail() {
        return getUser().getEmail();
    }

    /**
     * 获取当前登录用户是否在白名单
     *
     * @return 是否白名单
     */
    public static boolean isVipCustomer() {
        SaSession session = StpUtil.getSession();
        return session.get(SaTokenConstant.CUSTOMER_WHITE_LIST, () -> {
            WhiteListBizService service = QtApplicationContextHelper.getBean(WhiteListBizService.class);
            return service.exists(getUserEmail());
        });
    }

    /**
     * 获取当前登录用户的角色集合
     *
     * @return 角色集合
     */
    public static List<String> getUserRoles() {
        SaSession session = StpUtil.getSession();
        return session.get(SaSession.ROLE_LIST, () -> {
            UserRoleBizService service = QtApplicationContextHelper.getBean(UserRoleBizService.class);
            return service.getUserRoles(getUserId());
        });

    }

    /**
     * 获取指定 {@code loginId} 的角色集合
     *
     * @param loginId loginId
     * @return 角色集合
     */
    public static List<String> getUserRoles(Object loginId) {
        String userId = String.valueOf(loginId);
        SaSession session = StpUtil.getSessionByLoginId(loginId);
        return session.get(SaSession.ROLE_LIST, () -> {
            UserRoleBizService service = QtApplicationContextHelper.getBean(UserRoleBizService.class);
            return service.getUserRoles(userId);
        });
    }

    /**
     * 获取当前登录用户的信息
     *
     * @return 用户信息
     */
    public static SaUserInfoDTO getUser() {
        SaSession session = StpUtil.getSession();
        return session.get(SaSession.USER, () -> {
            SaUserInfoDTO saUserInfo = new SaUserInfoDTO();
            UserBizService userBizService = QtApplicationContextHelper.getBean(UserBizService.class);
            UserDTO userDTO = userBizService.getUserByUserId(getUserId());
            BeanUtil.copyProperties(userDTO, saUserInfo);
            return saUserInfo;
        });
    }

    /**
     * 获取当前登录用户的商店id
     *
     * @return sysShopIds
     */
    public static List<String> getUserShopIds() {
        SaSession session = StpUtil.getSession();
        return session.get(SaTokenConstant.CUSTOMER_SHOP_LIST, () -> {
            IShopService service = QtApplicationContextHelper.getBean(IShopService.class);
            return service.listShopIds(getUserId());
        });
    }

    public static boolean checkLoginUserShops(List<String> sysShopIds) {
        if (CollUtil.isEmpty(sysShopIds)) {
            return false;
        }

        List<String> userShopIds = getUserShopIds();
        if (CollUtil.isEmpty(userShopIds)) {
            return false;
        }
        return CollUtil.containsAll(userShopIds, sysShopIds);
    }

    public static boolean checkLoginUserShop(String sysShopIds) {
        return checkLoginUserShops(Lists.newArrayList(sysShopIds));
    }

    /**
     * 移除指定用户的 session key
     *
     * @param userId 用户id
     * @param key    key
     */
    public static void removeUserSessionKey(String userId, String key) {
        if (!StpUtil.isLogin(userId)) {
            return;
        }
        SaSession session = StpUtil.getSessionByLoginId(userId);
        session.delete(key);
    }

    /**
     * 是否客户登录
     *
     * @return 是否客户登录
     */
    public static boolean isCustomerRole() {
        return getUserRoles().contains(RoleConstant.CUSTOMER);
    }

    /**
     * 是否员工登录
     *
     * @return 是否员工登录
     */
    public static boolean isEmployeeRole() {
        return getUserRoles().contains(RoleConstant.EMPLOYEE);
    }
}
